package com.anruisi.hxmes.auth.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * @author cmy
 * @date 2021/7/14 13:38
 */
@Slf4j
@RestControllerAdvice
public class AuthExceptionHandler {

    /**
     * 用户名和密码错误
     *
     * @param e
     * @return
     */
    @ExceptionHandler(InvalidGrantException.class)
    public Map<String,Object> handleInvalidGrantException(InvalidGrantException e) {
        Map<String,Object> map = new HashMap<>(2);
        map.put("code",401);
        map.put("message",e.getMessage());
        return map;
    }

    /**
     * 账户异常(禁用、锁定、过期)
     *
     * @param e
     * @return
     */
    @ExceptionHandler({InternalAuthenticationServiceException.class})
    public Map<String,Object> handleInternalAuthenticationServiceException(InternalAuthenticationServiceException e) {
        Map<String,Object> map = new HashMap<>(2);
        map.put("code",401);
        map.put("message",e.getMessage());
        return map;
    }

    @ExceptionHandler({Exception.class})
    public Map<String,Object> otherException(Exception e) {
        Map<String,Object> map = new HashMap<>(2);
        map.put("code",500);
        map.put("message",e.getMessage());
        return map;
    }
}
